
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "BBViewer.h"

#include "MainFrm.h"
#include <HtmlHelp.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame
CMainFrame* g_pMainFrame = NULL;

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWndEx)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_MOVE()
	ON_WM_KEYUP()
	ON_COMMAND(ID_FILE_OPEN, &CMainFrame::OnFileOpen)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CMainFrame::OnPrintPreview)
	ON_COMMAND(ID_BACKUP, &CMainFrame::OnBackup)
	ON_COMMAND(ID_SETUP, &CMainFrame::OnSetup)
	ON_COMMAND(ID_VIEWER_SETUP, &CMainFrame::OnViewerSetup)
	ON_COMMAND(ID_MAXIMIZE, &CMainFrame::OnMaximize)
	ON_COMMAND(ID_FULL, &CMainFrame::OnFullScreen)
	ON_COMMAND(ID_DELETE, &CMainFrame::OnDelete)
	ON_COMMAND(ID_ZOOM_IN, &CMainFrame::OnOemPlus)
	ON_COMMAND(ID_ZOOM_OUT, &CMainFrame::OnOemMinus)
	ON_COMMAND(IDHELP, &CMainFrame::OnHtmlHelp)
END_MESSAGE_MAP()

// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	g_pMainFrame	= this;

	m_pUIMain		= NULL;
	m_bCreated		= FALSE;

	m_pUIMain		= new CUIMain;
}

CMainFrame::~CMainFrame()
{
	if (m_pUIMain)
	{
		delete  m_pUIMain;
		m_pUIMain = NULL;
	}

	g_pMainFrame	= NULL;
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	this->CenterWindow();

	if (!m_pUIMain->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, NULL, NULL))
	{		
		return -1;
	}

	m_bCreated = TRUE;
	
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWndEx::PreCreateWindow(cs) )
		return FALSE;
	cs.x		  = 0;
	cs.y		  = 0;
	cs.cx		  = theApp.dwWinWidth;
	cs.cy		  = theApp.dwWinHeight;
	cs.style	  = WS_VISIBLE | WS_POPUP;
	cs.hMenu	  = NULL;
	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.lpszName   = _T("BBViewer");

	return TRUE;
}

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWndEx::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWndEx::Dump(dc);
}
#endif //_DEBUG


// CMainFrame message handlers


void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	CFrameWndEx::OnSize(nType, cx, cy);

	if (m_bCreated)
	{
		m_pUIMain->MoveWindow(0, 0, cx, cy);

		CRect rc;

		GetWindowRect(&rc);

		CRgn rgn;
		LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpRoundRgn);

		rgn.CreateRoundRectRgn(0, 0, rc.right - rc.left + 1, rc.bottom - rc.top + 1, lpWndPos->rtWndPos.Width(), lpWndPos->rtWndPos.Height());
		::SetWindowRgn(m_hWnd, (HRGN)rgn, TRUE);
		rgn.DeleteObject();
	}
}

void CMainFrame::OnMove(int x, int y)
{
	CFrameWndEx::OnMove(x, y);

	RECT rc;
	GetWindowRect(&rc);

	if (m_bCreated)
	{
		CRgn rgn;
		LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpRoundRgn);
		rgn.CreateRoundRectRgn(0, 0, rc.right - rc.left + 1, rc.bottom - rc.top + 1, lpWndPos->rtWndPos.Width(), lpWndPos->rtWndPos.Height());
		::SetWindowRgn(m_hWnd, (HRGN)rgn, TRUE);
		rgn.DeleteObject();
	}
}

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	WORD wFlag = HIWORD(pMsg->lParam);
	WORD wKey = LOWORD(pMsg->wParam);
	LPST_BMP_INFO lpBmpInfo;


	if ((wFlag & KF_ALTDOWN)&&(wKey == VK_F4))
		return TRUE;

	if (wFlag&KF_UP)
	{
		switch (wKey)
		{
		case VK_ESCAPE:
			if (m_pUIMain->GetFullScreen())
				OnFullScreen();
			break;
		case VK_PRIOR:
			lpBmpInfo = theApp.BmpInfo.GetAt(biPrevFile_normal);
			m_pUIMain->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case VK_NEXT:
			lpBmpInfo = theApp.BmpInfo.GetAt(biNextFile_normal);
			m_pUIMain->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case _T('f'):
		case _T('F'):
			lpBmpInfo = theApp.BmpInfo.GetAt(biStepForward_normal);
			m_pUIMain->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case _T('t'):
		case _T('T'):
			lpBmpInfo = theApp.BmpInfo.GetAt(biTabTime_unchecked);
			m_pUIMain->GetWndTab()->SetTab(0);
			m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case _T('e'):
		case _T('E'):
			lpBmpInfo = theApp.BmpInfo.GetAt(biTabEvent_unchecked);
			m_pUIMain->GetWndTab()->SetTab(1);
			m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case _T('c'):
		case _T('C'):
			lpBmpInfo = theApp.BmpInfo.GetAt(biPB_faster_normal);
			m_pUIMain->GetWndPbSpeed()->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case _T('x'):
		case _T('X'):
			lpBmpInfo = theApp.BmpInfo.GetAt(biPB_slower_normal);
			m_pUIMain->GetWndPbSpeed()->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case VK_RIGHT:
			lpBmpInfo = theApp.BmpInfo.GetAt(biStepForward_k_normal);
			m_pUIMain->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case VK_LEFT:
			lpBmpInfo = theApp.BmpInfo.GetAt(biStepBackward_k_normal);
			m_pUIMain->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, 0);
			break;
		case VK_SPACE:
			m_pUIMain->PlayNPause();
			break;
		case VK_BACK:
			m_pUIMain->StopNPlay();
			break;
		}
	}

	return CFrameWndEx::PreTranslateMessage(pMsg);
}

void CMainFrame::KeyControl(WORD wFlag, WORD wKey)
{

}


LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	if (WM_SETTINGCHANGE == message)
	{
		m_pUIMain->PostMessage(WM_CTL_MESSAGE, WM_USER_DISPLAYCHANGE);
	}
	else if (WM_VIEWER_SETUP == message)
	{
		m_pUIMain->PostMessage(WM_VIEWER_SETUP);
	}

	return CFrameWndEx::WindowProc(message, wParam, lParam);
}

void CMainFrame::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{

	CFrameWndEx::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CMainFrame::OnFileOpen()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biFileOpen_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnPrintPreview()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biPrint_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnBackup()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biBackup_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnSetup()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biSetup_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnViewerSetup()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biViewer_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnMaximize()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biMax_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnFullScreen()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biFull_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnDelete()
{
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biDelete_normal);
	m_pUIMain->PostMessage(WM_CTL_MESSAGE, lpBmpInfo->uImageID, 0);
}

void CMainFrame::OnOemPlus()
{
}

void CMainFrame::OnOemMinus()
{
}

void CMainFrame::OnHtmlHelp()
{
	TCHAR tzHelpPath[MAX_PATH];
	ZeroMemory(tzHelpPath, sizeof(tzHelpPath));
	wsprintf(tzHelpPath, _T("%s\\BBViewer.chm"), theApp.m_strLangPath);
	HWND hWnd = ::HtmlHelp(GetDesktopWindow()->GetSafeHwnd(), tzHelpPath, HH_DISPLAY_TOPIC, 0);

	if (hWnd == NULL)
	{
		AfxMessageBox(_T("No BBViewer.chm file is found"));
	}
}